package com.iqiyi.controller;

import cn.hutool.core.bean.BeanUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.iqiyi.common.R;
import com.iqiyi.domain.*;
import com.iqiyi.entity.ActorVo;
import com.iqiyi.entity.MovieVo;
import com.iqiyi.service.*;
import com.iqiyi.service.impl.ActorServiceImpl;
import com.sun.scenario.effect.impl.sw.sse.SSEBlend_SRC_OUTPeer;
import io.swagger.models.auth.In;
import org.apache.tomcat.util.codec.binary.Base64;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.security.interfaces.RSAKey;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @auther Z_zjl
 * @date 2022/12/18 19:06
 * @Description
 */
@RestController
@RequestMapping("actor")
@CrossOrigin(value = "http://localhost:8080")
public class ActorController {
    @Resource
    private ActorService actorService;
    @Resource
    private ActorRegionService actorRegionService;
    @Resource
    private RegionService regionService;
    @Resource
    private MovieService movieService;
    @Resource
    private MovieActorService movieActorService;
    @Value("${iqiyi.path}")
    private String uploadDir;
    /**
     * 演员列表，返回一个ActorVo列表(里面有一个region列表)
     * @return
     */
    @GetMapping("/findAll")
    public List<ActorVo> findAll(){
        // 此方法是用xml.mapper实现的
        // actorService.findAll();

        //TODO 1.查询出所有演员
        List<Actor> actorList = actorService.list();
        List<ActorVo> ActorVoList = new ArrayList<>();

        //TODO 2.在演员、区域中间表中找到所有的记录
        for (Actor actor: actorList){
            ActorVo actorVo = BeanUtil.copyProperties(actor, ActorVo.class);
            List<ActorRegion> actorRegionList = actorRegionService.list(Wrappers.lambdaQuery(ActorRegion.class).eq(ActorRegion::getActorid, actor.getId()));
            List<Region> regionList = new ArrayList<>();
            for (ActorRegion ar : actorRegionList){
                Region region = regionService.getOne(Wrappers.lambdaQuery(Region.class).eq(Region::getId, ar.getRegionid()));
                regionList.add(region);
            }
            actorVo.setRegions(regionList);
            ActorVoList.add(actorVo);
        }
        return ActorVoList;
    }


    /**
     * 在编辑‘电影演员’的时候需要访问此接口
     * 需要返回电影、以及主演。
     * @return
     */
    @GetMapping("/findMovieAndActors")
    public Map<String,Object> findMovieAndActors(int movieId){
        List<Actor> allActor = actorService.list();

        //TODO 1.查出所有电影
        List<Movie> movieList = movieService.list();
        ArrayList<MovieVo> movieVoList = new ArrayList<>();
        Movie movie = movieService.getOne(Wrappers.lambdaQuery(Movie.class).eq(Movie::getId, movieId));
        //for(Movie movie : movieList){
            MovieVo movieVo = BeanUtil.copyProperties(movie, MovieVo.class);//复制属性到MovieVo
            //TODO 2.在电影、演员中间表中查出有该电影ID的记录
            //TODO 2.1 得到中间数据中电影ID相符合的记录
            List<MovieActor> movieActorList = movieActorService.list(Wrappers.lambdaQuery(MovieActor.class).eq(MovieActor::getMovieid, movie.getId()));
            List<Actor> actors = new ArrayList<>();
            for (MovieActor ma: movieActorList){
                //TODO 2.2. 得到演员符合的记录
                Actor actor = actorService.getOne(Wrappers.lambdaQuery(Actor.class).eq(Actor::getId, ma.getActorid()));
                actors.add(actor);
            }
            movieVo.setActors(actors);
            movieVoList.add(movieVo);
        //}
        Map<String, Object> dataMap = new HashMap<>();
        dataMap.put("movieVo",movieVo);
        dataMap.put("allActor",allActor);
        return dataMap;
    }

    /**
     * 查找所有的电影，在'添加电影演员'的时候，需要先访问此接口，页面上的下拉框需要显示出所有的电影和演员供选择
     * @return
     */
    @GetMapping("/editMovieActor")
    public Map<String,Object> editMovieActor(){
        List<Movie> movies = movieService.list();
        List<Actor> actors = actorService.list();
        Map<String, Object> dataMap = new HashMap<>();
        dataMap.put("movies",movies);
        dataMap.put("actors",actors);
        return dataMap;
    }

    /**
     * 编辑'电影演员',更新电影演员表
     * @return
     */
    @PostMapping("/saveMovieActor")
    public String saveMovieActor(@RequestBody MovieVo movieVo){
        Movie movie = BeanUtil.copyProperties(movieVo, Movie.class);
        // 得到类型列表
        List<Actor> actors = movieVo.getActors();
        // 先根据电影id删除电影演员表中的数据
        movieActorService.remove(Wrappers.lambdaQuery(MovieActor.class).eq(MovieActor::getMovieid, movie.getId()));
        // 再插入新的数据
        actors.forEach(actor -> {
            MovieActor movieActor = new MovieActor();
            movieActor.setMovieid(movie.getId());
            movieActor.setActorid(actor.getId());
            movieActorService.save(movieActor);
        });
        return null;
    }

    /**
     * 添加电影演员接口
     * 这个页面上：先选择一部电影，然后为该电影添加演员
     * @param movieVo
     * @return
     */
    @PostMapping("/addMovieActor")
    public String addMovieActor(@RequestBody MovieVo movieVo){

        int movieId = movieVo.getId();
        List<Actor> actors = movieVo.getActors();

        //直接在电影、演员的中间表里保存数据即可
        actors.forEach(actor -> {
            MovieActor movieActor = new MovieActor();
            movieActor.setActorid(actor.getId());
            movieActor.setMovieid(movieId);
            movieActorService.save(movieActor);
        });
        return "已保存";
    }


    /**
     * 删除电影的主演
     * @param movieId
     * @return
     */
    @GetMapping("/deleteMovieActor")
    public String deleteMovieActor(Integer movieId){
        // 删除该电影的全部主演
        //直接删除电影、演员的中间表里的数据即可
        movieActorService.remove(Wrappers.lambdaQuery(MovieActor.class).eq(MovieActor::getMovieid, movieId));
        return "已删除";
    }
    //在演员表中添加演员
    //两个参数分别是actor对象存actor表，actorvo对象存与region的连表，actorService.saveActor(actor)
    @PostMapping("/addActoractor")
    public String addActoractor(@RequestBody ActorVo actorVo){
        Actor actor = BeanUtil.copyProperties(actorVo, Actor.class);
        List<Region> regions = actorVo.getRegions();
        if(actorService.saveActor(actor)){
            for (Region region:regions) {
                ActorRegion actorRegion=new ActorRegion();
                actorRegion.setActorid(actor.getId());
                actorRegion.setRegionid(region.getId());
                System.out.println(actorRegion);
                actorRegionService.saveRegion(actorRegion);
            }
            return "添加成功";
        }else{
            return "添加失败";
        }
    }
    //点击编辑时需要返回的内容，查看一位演员的信息
    @GetMapping("/editorActor1")
    public Map<String,Object>editorActor1(int actorId){
        Map<String,Object>map=new HashMap<>();
        List<ActorVo> actorvo=actorService.getActorByid(actorId);
        List<Region> regions=regionService.findAll();
        map.put("actorvo",actorvo);
        map.put("regions",regions);
       return map;
    }

    //在编辑点开后显示相关信息，对信息进行修改。
    @PostMapping("/editorActor2")
    public void editorActor2(@RequestBody ActorVo actorVo){
        actorService.updateActor(actorVo);
    }

    //连表删除，将与此条记录有关的外表记录删除
    @GetMapping("/delActor")
    public String delActor(int id){
        // 删除电影演员表中的记录
        movieActorService.remove(Wrappers.lambdaQuery(MovieActor.class).eq(MovieActor::getActorid,id));
        // 删除演员表和演员区域表中记录
        if(actorService.delActor(id)){
            return "删除成功";
        }else{
            return "删除失败";
        }
    }
}
